﻿using MQTTnet.Protocol;
using MQTTnet.Server;
using MQTTnet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace MyMqtt
{
    public static class MqttSeverHelper
    {
      //  public static event Func<InjectedMqttApplicationMessage, CancellationToken,Task > PublishEvent;

        //public static void Publish(string topic, string data, int leve = 0, bool retain = true)
        //{
        //    if (PublishEvent == null) return;
        //    var message = new InjectedMqttApplicationMessage(new MqttApplicationMessage()
        //    {
        //        Topic = topic,
        //        Payload = Encoding.UTF8.GetBytes(data),
        //        QualityOfServiceLevel = (MqttQualityOfServiceLevel)leve,
        //        Retain = retain  // 服务端是否保留消息。true为保留，如果有新的订阅者连接，就会立马收到该消息。
        //    });
        //   // PublishEvent(message);
        //}

        public static Task OnClientConnected(ClientConnectedEventArgs eventArgs)
        {
            Console.WriteLine($"Client '{eventArgs.ClientId}' connected.");
            return Task.CompletedTask;
        }


        public static Task ValidateConnection(ValidatingConnectionEventArgs eventArgs)
        {
            Console.WriteLine($"Client '{eventArgs.ClientId}' wants to connect. Accepting!");
            return Task.CompletedTask;
        }
        //客户端订阅
        public static Task ClientSubscribedTopicAsync(ClientSubscribedTopicEventArgs arg)
        {

            return Task.CompletedTask;
        }
        //去掉订阅
        public static Task ClientUnsubscribedTopicAsync(ClientUnsubscribedTopicEventArgs arg)
        {
            return Task.CompletedTask;
        }
        //发布
        public static Task InterceptingPublishAsync(InterceptingPublishEventArgs arg)
        {
            return Task.CompletedTask;
        }
    }

}
